package com.vgulu.server.common.utils;

import java.io.ByteArrayOutputStream;

/**
 * Created by Administrator on 2014/7/22 0022.
 */
public class TransformInHexAndString {

    private static String hexString="0123456789abcdef";

    public static String stringToHexStr(byte [] bytes){
        StringBuilder sb=new StringBuilder(bytes.length*2);
        for(int i=0;i<bytes.length;i++)
        {
            sb.append(hexString.charAt((bytes[i]&0xf0)>>4));
            sb.append(hexString.charAt((bytes[i]&0x0f)>>0));
        }
        return sb.toString();
    }

    public static byte[] hexStrToString(String hexStr){
        ByteArrayOutputStream out=new ByteArrayOutputStream(hexStr.length()/2);
        for(int i=0;i<hexStr.length();i+=2)
            out.write((hexString.indexOf(hexStr.charAt(i))<<4 |hexString.indexOf(hexStr.charAt(i+1))));
        return out.toByteArray();
    }

    public static void main(String[] args){
        System.out.println(new String(hexStrToString("77656e6c654c4f5645796966616e313331342140232324")).equals("wenleLOVEyifan1314!@##$"));
        System.out.println(new String(hexStrToString("900689406ebaafb5f1cfe90d25e2029dc6274616d16ae630af19a53c68a98e40")));
    }
}
